﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MQTTDemo
{
    internal class DeviceOnlineInfo
    {
        /// <summary>
        /// 当 timeoutInterval 秒没收到设备的信息时，即认为离线
        /// </summary>
        readonly int timeoutInterval_s = 60; 
        string mac;
        DateTime lastOnlineTime = DateTime.Now;
        public DeviceOnlineInfo() { }

        public DeviceOnlineInfo(string mac) { 
            this.mac = mac;
        }

        public string GetMac()
        {
            return mac;
        }

        /// <summary>
        /// 收到一次信息，即执行一次设置
        /// </summary>
        public void SetLastOnlineTime() { 
            this.lastOnlineTime = DateTime.Now;
        }

        /// <summary>
        /// 检查通讯是否已超时，超时，即返回true，否则返回false
        /// </summary>
        /// <returns></returns>
        public bool IsTimeout() { 
            TimeSpan ts = DateTime.Now - lastOnlineTime;
            if(ts.TotalSeconds > this.timeoutInterval_s)
            {
                return true;
            }
            return false;
        }
    }
}
